package org.destiny.swing.mapping.base;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;

import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class BooleanInputMapper extends AbstractFormItemMapper<Boolean> {
	private JPanel radioButtonPane;

	public BooleanInputMapper(Field field) {
		super(field);
		radioButtonPane = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
		ButtonGroup group = new ButtonGroup();
		final JRadioButton yesRadio = new JRadioButton("是", true);
		final JRadioButton noRadio = new JRadioButton("否");
		yesRadio.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Boolean newFv = yesRadio.isSelected();
				if (newFv != getFieldValue()) {
					setFieldValue(newFv);
				}
			}
		});
		radioButtonPane.add(yesRadio);
		radioButtonPane.add(noRadio);
		group.add(yesRadio);
		group.add(noRadio);
	}

	@Override
	public JComponent getInputComponent() {
		return radioButtonPane;
	}

}
